#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

/* 
 * 进程名相同，有问题,下一个程序解决
 * char name[64] 属于主线程中栈区之上的变量，
 * 多个线程实际指向的是同一块空间，最后一次覆盖后，所有线程都打印 thread-5
 */

using namespace std;

#define NUM 5

void * threadRun(void * name)
{
	while (true) {
		cout << "I am other" << (char*)name << endl;
		sleep(1);
	}

	return NULL;
}

int main()
{
	pthread_t pt[NUM];

	for (int i = 0; i < NUM; i++) {
		char name[64];
		snprintf(name, sizeof(name), "thread-%d", i+1);
		pthread_create(pt+i, NULL, threadRun, name);
	}

	while (true) {
		cout << "I am main thread, run..." << endl;
		sleep(1);
	}

	return 0;
}
